<template>
  <div>
    <div class="title">
      <div class="titleHint">
        <p>名称</p>
        <span v-if="seatState ? true : false">最多输入5个字符</span>
      </div>
      <div>序号</div>
      <div
        class="edit"
        v-if="!seatState ? true : false"
        @click="switchover('1')"
      >编辑</div>
      <div
        class="edit"
        v-if="seatState ? true : false"
        @click="switchover('0')"
      >新建</div>
    </div>
    <!-- 开关 -->
    <div class="subject" v-if="!seatState ? true : false">
      <div class="subjectBlock" v-for="(item,index) in seatList" :key="index">
        <div>{{item.seatName}}</div>
        <div>{{item.seatSort}}</div>
        <div>
          <span>{{item.vacancy === 'Y' ? '未使用' : '已使用'}}</span>
          <i-switch
            :value="item.vacancy === 'Y' ? false : true "
            @change="onChange(item.seatId,item.vacancy)"
            slot="footer"
          ></i-switch>
        </div>
      </div>
    </div>
    <!-- 编辑 -->
    <div class="subject2" v-if="seatState ? true : false">
      <div class="subjectBlock" v-for="(item,index) in seatList" :key="index">
        <div>
          <!-- 在列表页不需要编辑信息了，如果需要，之后放开input注释 -->
          <input
            class="input"
            v-model="item.seatName"
            :value="item.seatName"
            maxlength="5"
            v-if="!item.seatId"
            @change="seatNameChange"
          >
          <label v-else>{{item.seatName}}</label>
          <!-- {{item.seatName}} -->
        </div>
        <div>
          <!-- 在列表页不需要编辑信息了，如果需要，之后放开input注释 -->
          <input
            class="input"
            v-model="item.seatSort"
            :value="item.seatSort"
            maxlength="5"
            type="number"
            v-if="!item.seatId"
            @change="seatSortChange"
          >
          <label v-else>{{item.seatSort}}</label>
        </div>
        <div @click="deleteClick(item)">
          <span>×</span>
          <p>删除</p>
        </div>
        <div class="updateItem" @click="updateItem(item)">
          <span></span>
          <p>编辑</p>
        </div>
      </div>
    </div>
    <div class="kong"></div>
    <!-- 按钮box -->
    <div class="buttonBox">
      <button
        v-if="!seatState ? true : false"
        class="buttonBtn"
        @click="handleClick"
        type="primary"
        size="small"
      >完成</button>
      <button
        v-if="seatState ? true : false"
        class="buttonBtn"
        @click="AddClick"
        type="primary"
        size="small"
      >确定新增</button>
    </div>
  </div>
</template>

<script>
import {
  FIND_SHOP_SEAT_LIST_API,
  UPDATE_SHOP_SEAT_API,
  ADD_SHOP_SEAT_API,
  DELETE_SHOP_SEAT_API
} from '@/api'

export default {
  components: {},
  data () {
    return {
      seatState: false,
      seatList: [],
      seatListInit: []
    }
  },
  created () {},
  computed: {
    account () {
      return this.$cookie.get('account')
    }
  },
  onShow () {
    this.init()
    this._findShopSeatList()
  },
  methods: {
    switchover (value) {
      const vm = this
      if (value === '1') {
        vm.seatState = true
      } else {
        vm.seatList.unshift({
          seatSort: '',
          seatName: ''
        })
      }
    },
    onChange (value) {
      const vm = this
      for (let i = 0; i < vm.seatList.length; i++) {
        if (value === vm.seatList[i].seatId) {
          if (vm.seatList[i].vacancy === 'Y') {
            vm.seatList[i].vacancy = 'N'
            vm._setvacancy(value, vm.seatList[i].vacancy)
          } else {
            vm.seatList[i].vacancy = 'Y'
            vm._setvacancy(value, vm.seatList[i].vacancy)
          }
        }
      }
    }, // 是否使用切换
    _findShopSeatList () {
      const vm = this
      let shopId = vm.account.shopId
      FIND_SHOP_SEAT_LIST_API(shopId)
        .then(r => {
          if (r.data.code === '0') {
            vm.seatList = r.data.data
          } else {
            vm.$toast(r.data.message)
          }
        })
        .catch(e => {
          console.info(e)
        })
    }, // 获取商品座位信息
    _setvacancy (seatId, vacancy) {
      const vm = this
      let params = {
        seatId: seatId,
        shopId: vm.account.shopId,
        vacancy: vacancy
      }
      UPDATE_SHOP_SEAT_API(params)
        .then(r => {
          if (r.data.code === '0' && r.data.data) {
            vm.$toast('座位状态更新成功~~~')
          } else {
            vm.$toast(r.data.message)
          }
        })
        .catch(e => {
          console.info(e)
        })
    }, // 更改使用情况
    handleClick () {
      this.$router.go(-1)
    }, // 完成按钮
    AddClick () {
      const vm = this

      for (let i = 0; i < vm.seatList.length; i++) {
        if (vm.seatList[i].seatId === undefined) {
          vm.seatListInit.push(vm.seatList[i])
        }
      }
      let params = {
        shopId: vm.account.shopId,
        shopSeatList: vm.seatListInit.map(_ => ({
          seatSort: _.seatSort,
          seatName: _.seatName
        }))
      }
      ADD_SHOP_SEAT_API(params)
        .then(r => {
          if (r.data.code === '0' && r.data.data) {
            vm.$alert('操作成功', '').then(() => {
              vm.$router.go(-1)
            })
          } else {
            vm.$toast(r.data.message)
          }
        })
        .catch(e => {
          console.info(e)
        })
    }, // 确定新增
    deleteClick (item) {
      const vm = this
      vm.$dialog('确定删除').then(r => {
        if (item.seatId && item.shopId) {
          vm._deleteFun(item.seatId, item.shopId)
        } else {
          vm.seatList.forEach(_ => {
            if (_.seatId === item.seatId) {
              vm.seatList.splice(_, 1)
            }
          })
        }
      })
    }, // 假删除
    _deleteFun (seatId, shopId) {
      DELETE_SHOP_SEAT_API(seatId, shopId)
        .then(r => {
          if (r.data.code === '0' && r.data.data) {
            this.$toast('删除成功')
            this._findShopSeatList()
          } else {
            this.$toast(r.data.message)
          }
        })
        .catch(e => {
          console.info(e)
        })
    }, // 删除请求
    updateItem (item) {
      const vm = this
      console.log(item)
      if (item.seatId && item.shopId) {
        let val = JSON.stringify(item)
        vm.$router.push(
          '/pages/shop-management/shop-fitment-tow-update/main?val=' + val
        )
      } else {
        vm.$toast('新建信息不能编辑~~~')
      }
    },
    init () {
      const vm = this
      vm.seatList = []
      vm.seatState = false
    }
  }
}
</script>

<style lang="scss">
@import 'static/styles/color.scss';
.title {
  display: flex;
  justify-content: space-between;
  align-content: center;
  width: 100%;
  height: 50px;
  div {
    width: 33.33%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 16px;
    color: #000;
  }
  .titleHint {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    position: relative;
    span {
      font-size: 12px;
      color: #9b9b9b;
      position: absolute;
      bottom: 0;
    }
  }
  .edit {
    color: #3e81fb;
  }
}
.subject {
  .subjectBlock {
    display: flex;
    justify-content: space-between;
    align-items: center;
    div {
      width: 33.33%;
      height: 80px;
      font-size: 14px;
      color: #5f5f5f;
      display: flex;
      justify-content: center;
      align-items: center;
      span {
        margin-right: 20px;
      }
    }
  }
}
.subject2 {
  .subjectBlock {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .input {
      color: #606266;
      font-size: 14px;
      border: 1px solid #dcdfe6;
      border-radius: 4px;
      text-align: center;
      padding: 7px 15px;
      height: 30px;
      line-height: 30px;
    }
    div {
      width: 33.33%;
      height: 80px;
      font-size: 14px;
      color: #5f5f5f;
      display: flex;
      justify-content: center;
      align-items: center;
      span {
        width: 20px;
        height: 20px;
        font-size: 12px;
        background: #ff2d2d;
        color: #fff;
        display: block;
        line-height: 20px;
        text-align: center;
        border-radius: 50px;
        margin-right: 5px;
      }
      p {
        font-size: 12px;
        color: #ff2d2d;
      }
    }
    .updateItem {
      span {
        background: #3ece46;
      }
      p {
        color: #3ece46;
      }
    }
  }
}
.kong {
  height: 90px;
}
.buttonBox {
  width: 100%;
  height: 40px;
  position: fixed;
  bottom: 0;
  z-index: 2;
  .buttonBtn {
    width: 100%;
    border-radius: 0;
    text-align: center;
    vertical-align: middle;
    touch-action: manipulation;
    cursor: pointer;
    white-space: nowrap;
    user-select: none;
    line-height: 40px;
    font-size: 14px;
    border: 0 !important;
    text-decoration: none;
    color: #fff !important;
    background: #508ff9;
  }
}
</style>
